package com.google.android.daca;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.daca.service.DoodleRESTService;
import com.google.android.daca.types.Poll;
import com.google.android.daca.types.States;

/**
 * This screen is used for closing polls. Poll is closed by providing its 16-char long poll
 * identifier and 8-char long admin key
 * 
 * @author dyanko
 */
public class ClosePollActivity extends Activity {

	private static final String CLASS_TAG = ClosePollActivity.class.getSimpleName();
	private EditText pollID;
	private EditText adminKeyID;
	private Button closePoll;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Log.v(CLASS_TAG, "Enter ClosePollActivity.onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.close_poll);
		pollID = (EditText) findViewById(R.id.poll_id_key);
		String doodlePollID = getIntent().getStringExtra(getResources().getString(R.string.extra_poll_id));
		if (doodlePollID != null)
		{
			pollID.setText(doodlePollID);
		}
		adminKeyID = (EditText) findViewById(R.id.admin_key);
		
		// click Close Poll to close current poll
        closePoll = (Button) findViewById(R.id.close_button);
        closePoll.setOnClickListener(new OnClickListener() 
        	{
				public void onClick(View v) 
				{
					 closePoll();
				}
        	}
        );
	}
	
	/**
     * Close poll specified by poolID. Admin key required
     * for closing the poll.
     */
	private void closePoll() {
		//Validate user input
		if(adminKeyID.getText() == null || adminKeyID.getText().toString().trim().length() == 0
				|| pollID.getText() == null || pollID.getText().toString().trim().length() == 0) {
			Log.v(CLASS_TAG, "Admin key not provided.");
			String validationText = getResources().getString(R.string.admin_key_not_defined);
			new AlertDialog.Builder(this).setTitle(
		            	getResources().getString(R.string.alert_label)).setMessage(
		            		validationText.toString()).setPositiveButton("Continue",
		            							                         new android.content.DialogInterface.OnClickListener() 
														                 {				
														                     public void onClick(DialogInterface dialog, int arg1) 
														                     {
														                         // in this case, don't need to do anything other than close alert
														                     }
														                 }).show();
			return;
		}
		
		final DoodleRESTService dRs = new DoodleRESTService();
		
		try {
			
			//First get the poll that is going to be closed
			Poll currentPoll = dRs.getPollByID(pollID.getText().toString());
			//Change the state of the poll to CLOSED
			currentPoll.setState(States.CLOSED);
			//Save the new state and close the poll
			dRs.editPoll(currentPoll, pollID.getText().toString(), adminKeyID.getText().toString());
			Toast.makeText(getApplicationContext(),
					"Poll closed successfully.", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Log.e(CLASS_TAG, "Admin key not valid. Cannot close poll.");
			Toast.makeText(
					getApplicationContext(),
					"Poll ID or Admin Key not valid!", Toast.LENGTH_LONG)
					.show();
			e.printStackTrace();
		}
	}
}
